package com.rzw.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.*;

/**
 * Created by DELL on 2017/10/11.
 */
public class HttpUtils {
    public static String sendPost(String url, Map<String, String> paramsMap) throws IOException {
        //创建httpClient
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //创建httpPost
        HttpPost httpPost = new HttpPost(url);

        //拼接参数
        if (paramsMap!=null && paramsMap.size()>0){
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            Set<Map.Entry<String, String>> entrySet = paramsMap.entrySet();
            for (Map.Entry<String,String> entry: entrySet) {
                if (StringUtils.isNoneBlank(entry.getValue())){
                    nameValuePairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
                }
            }
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
            //执行
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity entity = httpResponse.getEntity();
            String responseContent = EntityUtils.toString(entity);
            EntityUtils.consume(entity);
            httpClient.close();
            return responseContent;
        }
        return null;
    }

    /**
     * 带参数的post方法
     *
     * @param url   请求地址
     * @param param 请求参数
     * @return
     * @throws Exception
     */
    public static String doPost(String url, Map<String, Object> param) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建httpPost
        HttpPost http = new HttpPost(url);

        if (param != null) {
            List<NameValuePair> parameters = new ArrayList<NameValuePair>();
            Set<Map.Entry<String, Object>> entrySet = param.entrySet();
            for (Map.Entry<String, Object> entry : entrySet) {
                if (entry.getValue() != null) {
                    parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
                }
            }

            //设置参数
            http.setEntity(new UrlEncodedFormEntity(parameters, "utf-8"));
        }

        CloseableHttpResponse httpResponse = null;
        try {
            // 执行
            httpResponse = httpClient.execute(http);
            // 处理结果
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String content = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
                return "200";
            }
        } finally {
            if (httpResponse != null) {
                httpResponse.close();
            }
            httpClient.close();
        }
        return "OK";
    }

    public static void main(String[] args) throws IOException {
        HashMap<String, String> paramsMap = new HashMap<String, String>();
        paramsMap.put("userName","李雷");
        paramsMap.put("password","韩梅梅");
        String res = HttpUtils.sendPost("http://localhost:9090/hello_word/user/test1", paramsMap);
        System.out.println("res：---"+res);
    }
}